<%@ page language="java" import="java.util.*" pageEncoding="UTF-8" %>
<%@ include file="/wesite/view/common/base.jsp" %>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport"
          content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0"/>
    <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
    <title>活动专区</title>
    <%@ include file="/wesite/view/common/common.jsp" %>
</head>
<body>
<div class="active-box">

    <c:if test="${we_active_ad != null}">
        <div class="active-ad">
            <a href="javascript:;">
                <img src="<%=getRandomFileserver()%>${we_active_ad}">
            </a>
        </div>
    </c:if>
    <!-- 单品促销 -->
    <div class="active-list" id="flash_sale">

    </div>

    <!-- 买送促销 -->
    <div class="active-list" id="buy_send">

    </div>

    <!-- 满赠促销 -->
    <div class="active-list" id="purchase_gift">

    </div>

</div>
<%@ include file="/wesite/view/common/popup-menu.jsp"%>
</body>
<script>
    function setFlashSalesData(fs) {
        if (fs && fs.length > 0) {
            $("#flash_sale").show();
            var itemHtml = "";
            itemHtml += '<div class="active-item-ad"><a href="javascript:;"><img src="https://file2.judayw.com/info/17070414112906960.png"></a></div>';
            for (var i = 0; i < fs.length; i++) {
                var fsItem = fs[i];
                itemHtml += '<div class="active-item"><div class="active-item-time"><span class="fl">' + fsItem.description + '</span>';
                    + '<span class="fr endTime"></span></div>';
                if(fsItem.amount <= 0) {
                    itemHtml += ("<p class='endtime fr'><em>" + fsItem.soldAmount + "</em> 件单品已抢完啦！</p></div>");
                }else {
                    if(fsItem.countDown == '1') {
                        itemHtml += ("<span endTime='" + fsItem.surplusSeconds + "' class='endtime fr'></span></div>");
                    }else {
                        itemHtml += ("<p class='endtime fr'>活动火热进行中</p></div>");
                    }
                }
                itemHtml += '<ul><li>'
                    + '<a class="active-img" href="/product/info?priceid=' + fsItem.id + '"><img src="' + getRandomFileserver() + fsItem.photo + '" alt="">';
                if (fsItem.amount <= 0) {
                    itemHtml += '<span class="msk-bg"></span><span class="msk-txt">已抢完</span>';
                }
                itemHtml += '</a><div class="active-text fl"><p class="text-name">'+ fsItem.name + '</p><p class="text-company txt">'
                    + fsItem.changjia + '</p><p class="text-st txt">' + fsItem.guige + '</p>';
                if (fsItem.iscontrol == 0) {
                    if (fsItem.canPromotion == 1) {
                        itemHtml += (((isuserauth && islogin) ?'<p class="text-price">¥' + parseFloat(fsItem.fsPrice) + '<span class="dp">¥' + parseFloat(fsItem.price) + "</span></p>" : (!islogin ? "<p class='text-msg'>登录可见</p>" : "<p class='text-price'>审核通过可见</p>")));
                    } else {
                        itemHtml += (((isuserauth && islogin) ? '<p class="text-price">¥' + parseFloat(fsItem.price)  + "</p>" : (!islogin ? "<p class='text-msg'>登录可见</p>" : "<p class='text-msg'>审核通过可见</p>")));
                    }
                } else {
                    itemHtml += (((isuserauth && islogin) ? "<p class='text-msg'>不在本区域销售</p>" : (!islogin ? "<p class='text-msg'>登录可见</p>" : "<p class='text-msg'>审核通过可见</p>")));
                }

                if (islogin && isuserauth && fsItem.iscontrol == 0 && fsItem.price > 0 && (fsItem.times > 0 || fsItem.times === '') && fsItem.amount > 0) {
                    itemHtml += '<a onclick="stopBubble(event,' + fsItem.id + ')" class="ico-shopping-cart" href="javascript:;"><img src="/wesite/images/ico-shopping-cart.png"></a>';
                } else {
                    itemHtml += '<a href="javascript:void(0);"  class="ico-shopping-cart" ><img src="/wesite/images/ico-shopping-cart-no.png"></a>';
                }

                itemHtml += '</div><div class="cl"></div></li></ul></div>';
            }
            $("#flash_sale").html(itemHtml);
        } else {
            $("#flash_sale").hide();
        }
    }
    function setBuySendData(bs) {
        if (bs && bs.length > 0) {
            $("#buy_send").show();
            var itemHtml = "";
            itemHtml += '<div class="active-item-ad"><a href="javascript:;"><img src="https://file2.judayw.com/info/17070414105530775.png"></a></div>';
            for (var i = 0; i < bs.length; i++) {
                var bsItem = bs[i];
                itemHtml += '<div class="active-item"><div class="active-item-time"><span class="fl">' + bsItem.description + '</span></div>';

                itemHtml += '<ul><li>'
                    + '<a class="active-img" href="/product/info?priceid=' + bsItem.id + '"><img src="' + getRandomFileserver() + bsItem.photo + '" alt="">';

                itemHtml += '</a><div class="active-text fl"><p class="text-name">'+ bsItem.name + '</p><p class="text-company txt">'
                    + bsItem.changjia + '</p><p class="text-st txt">' + bsItem.guige + '</p>';
                if (bsItem.iscontrol == 0) {
                    itemHtml += (((isuserauth && islogin) ?'<p class="text-price">¥' + parseFloat(bsItem.price) : (!islogin ? "<p class='text-msg'>登录可见</p>" : "<p class='text-msg'>审核通过可见</p>")));
                    if (bsItem.canPromotion == 1) {
                        itemHtml += '<span class="ms">买 ' + bsItem.standardNumber + ' 送 ' + bsItem.sendNumber + ' </span></p>';
                    } else {
                        itemHtml += '</p>';
                    }
                } else {
                    itemHtml += (((isuserauth && islogin) ? "<p class='text-msg'>不在本区域销售</p>" : (!islogin ? "<p class='text-msg'>登录可见</p>" : "<p class='text-msg'>审核通过可见</p>")));
                }

                if (islogin && isuserauth && bsItem.iscontrol == 0 && bsItem.price > 0 && (bsItem.times > 0 || bsItem.times === '') && bsItem.amount > 0) {
                    itemHtml += '<a onclick="stopBubble(event,' + bsItem.id + ')" class="ico-shopping-cart" href="javascript:;"><img src="/wesite/images/ico-shopping-cart.png"></a>';
                } else {
                    itemHtml += '<a href="javascript:void(0);"  class="ico-shopping-cart" ><img src="/wesite/images/ico-shopping-cart-no.png"></a>';
                }
                itemHtml += '</div><div class="cl"></div></li></ul></div>';


            }
            $("#buy_send").html(itemHtml);
        } else {
            $("#buy_send").css("display", "none");
        }
    }
    function setPurchaseGiftData(pg) {
        if (pg && pg.length > 0) {
            $("#purchase_gift").show();
            var itemHtml = "";
            itemHtml += '<div class="active-item-ad"><a href="javascript:;"><img src="https://file3.judayw.com/info/17070414115556474.png"></a></div>';
            for (var i = 0; i < pg.length; i++) {
                var pgItem = pg[i];
                itemHtml += '<div class="active-item"><div class="active-item-time"><span class="fl">' + pgItem.description + '</span></div>';

                itemHtml += '<ul><li>'
                    + '<a class="active-img" href="/product/info?priceid=' + pgItem.id + '"><img src="' + getRandomFileserver() + pgItem.photo + '" alt="">';

                itemHtml += '</a><div class="active-text fl"><p class="text-name">'+ pgItem.name + '</p><p class="text-company txt">'
                    + pgItem.changjia + '</p><p class="text-st txt">' + pgItem.guige + '</p>';
                if (pgItem.iscontrol == 0) {
                    if (pgItem.canPromotion == 1) {
                        itemHtml += (((isuserauth && islogin) ?'<p class="text-price">¥' + parseFloat(pgItem.price) : (!islogin ? "<p class='text-price'>登录可见</p>" : "<p class='text-price'>审核通过可见</p>")));
                        if (pgItem.canPromotion == 1) {
                            if (pgItem.minMoney != '') {
                                itemHtml += '<span class="ms">满 ' + pgItem.minMoney + ' 元送赠品</span></p>';
                            }else {
                                itemHtml += '<span class="ms">买 ' + pgItem.minNumber + ' ' +  pgItem.unitname + '送赠品</span></p>';
                            }
                        }else {
                            itemHtml += '</p>';
                        }
                    } else {
                        itemHtml += (((isuserauth && islogin) ? '<p class="text-price">¥' + parseFloat(pgItem.price)  + "</p>" : (!islogin ? "<p class='text-msg'>登录可见</p>" : "<p class='text-msg'>审核通过可见</p>")));
                    }
                } else {
                    itemHtml += (((isuserauth && islogin) ? "<p class='text-msg'>不在本区域销售</p>" : (!islogin ? "<p class='text-msg'>登录可见</p>" : "<p class='text-msg'>审核通过可见</p>")));
                }

                if (islogin && isuserauth && pgItem.iscontrol == 0 && pgItem.price > 0 && (pgItem.times > 0 || pgItem.times === '') && pgItem.amount > 0) {
                    itemHtml += '<a onclick="stopBubble(event,' + pgItem.id + ')" class="ico-shopping-cart" href="javascript:;"><img src="/wesite/images/ico-shopping-cart.png"></a>';
                } else {
                    itemHtml += '<a href="javascript:void(0);"  class="ico-shopping-cart" ><img src="/wesite/images/ico-shopping-cart-no.png"></a>';
                }
                itemHtml += '</div><div class="cl"></div></li></ul></div>';


            }
            $("#purchase_gift").html(itemHtml);
        } else {
            $("#purchase_gift").css("display", "none");
        }
    }
    $(function () {
        $w.httpRequest({
            url: "${ctx}/we/product/promotions",
            ok: function (ret) {
                setFlashSalesData(ret.Data.flash_sale);
               setBuySendData(ret.Data.buy_send);
               setPurchaseGiftData(ret.Data.purchase_gift);

            }
        });

        var flashSaleTimeInterval = window.setInterval(function () {
            var drugOvertimeItems = $(".fr[endTime]");
            if (drugOvertimeItems.length > 0) {
                drugOvertimeItems.each(function () {
                    var endTime = $(this).attr("endTime");
                    if (endTime > 0) {
                        var h = parseInt(endTime / (60 * 60));//小时
                        var m = parseInt(endTime / 60 % 60);//分钟
                        var s = parseInt(endTime % 60); //秒
                        $(this).html("疯抢倒计时：<i>" + h + "</i> 时 <i>" + m + "</i> 分 <i>" + s + "</i> 秒");
                        endTime--;
                        $(this).attr("endTime", endTime);
                    } else {
                        $(this).html("活动已截止");
                        $(this).removeAttr("endTime");
                    }
                });
            } else {
                window.clearInterval(flashSaleTimeInterval);
            }
        }, 1000);

    });
</script>
</html>
